home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 3_0 / MDEF__ / MDEF.C < prev    next >
C/C++ Source or Header  |  1988-01-02  |  5KB  |  195 lines

  1. #include "MacTypes.h"
  2. #include "QuickDraw.h"
  3. #include "MenuMgr.h"
  4. #include "ToolBoxUtil.h"
  5.  
  6. #define        PRECTWIDE        32        /* how wide are the patterns    */
  7. #define        PRECTHIGH        32        /* how high are the patterns     */
  8. #define        PATROWS            8        /* number of rows in menu        */
  9. #define        PATCOLS            5        /* number of columns in menu    */
  10. #define        LISTID            1000    /* Resource ID of PAT#            */
  11.  
  12. /**********************************************************************
  13.  
  14.     entry point for our <M>enu <DEF>inition
  15.     
  16.     This menu displays patterns taken from the System PAT#.
  17.     The size of the rectangles for each pattern are designated
  18.     by PRECTWIDE and PRECTHIGH.  The menu is drawn with the
  19.     rectangles, PATROWS high and PATCOLS wide, in this case,
  20.     8 patterns high by 5 patterns wide.
  21.     The MenuItem returned by the MDEF is the index within the
  22.     PAT# of the pattern chosen by the user, and can be found with...
  23.     
  24.         GetIndPattern(&somePatternVariable,LISTID,MenuItem);
  25.     
  26.     From the LightspeedC "Project" menu:
  27.     
  28.         "Set Project Type..."
  29.         ---------------------------------------
  30.                         Code Resource
  31.         Type             MDEF
  32.         Id                130
  33.         Name            Pattern Menu [optional]
  34.         
  35.         "Build Code Resource..."
  36.         "MDEF Code" if you're using the "XFER MDEF" project to
  37.         copy the MDEF and place it in the "patmenudemo.rsrc" file
  38. **********************************************************************/
  39.  
  40. pascal void main(message,theMenu,menuRect,hitPt,whichItem)
  41. int            message;
  42. MenuHandle    theMenu;
  43. Rect        *menuRect;
  44. Point        hitPt;
  45. int            *whichItem;
  46. {
  47.     /*
  48.         what does the menu manager want us to do?
  49.     */
  50.     switch(message){
  51.         case mDrawMsg:        /*
  52.                                 draw the menu
  53.                             */
  54.                             domenudraw(theMenu,menuRect);
  55.                             break;
  56.  
  57.         case mChooseMsg:    /*
  58.                                 user is choosing a menu item
  59.                             */
  60.                             domenuchoose(theMenu,menuRect,hitPt,whichItem);
  61.                             break;
  62.  
  63.         case mSizeMsg:        /*
  64.                                 tell the menu manager how large the menu
  65.                                 is when it is dropped down from the
  66.                                 menubar by filling in the menuHeight and
  67.                                 menuWidth fields of the MenuHandle data
  68.                             */
  69.                             domenusize(theMenu);
  70.                             break;
  71.  
  72.         default:            break;
  73.     }
  74. }
  75.  
  76. calcitemrect(MenuRect,resultRect,item)
  77. Rect    *MenuRect,*resultRect;
  78. int        item;
  79. {
  80.     int        row,col;
  81.  
  82.     /*
  83.         calculate the rectangle within the menu, (therefore, within MenuRect),
  84.         for any given menu item designated by "item".  Find out which row and
  85.         column the item is in, then construct the rectangle, and return the
  86.         rectangle as the function result
  87.     */
  88.     row = (item-1) / PATCOLS;
  89.     col = (item-1) % PATCOLS;
  90.     resultRect->left   = MenuRect->left + col * PRECTWIDE;
  91.     resultRect->top    = MenuRect->top  + row * PRECTHIGH;
  92.     resultRect->right  = resultRect->left + PRECTWIDE;
  93.     resultRect->bottom = resultRect->top + PRECTHIGH;
  94. }
  95.  
  96. domenudraw(theMenu,menuRect)
  97. MenuHandle    theMenu;
  98. Rect        *menuRect;
  99. {
  100.     Rect    itemRect;
  101.     Pattern    thePat;
  102.     int        index;
  103.     
  104.     /*
  105.         cycle through PATROWS * PATCOLS items, getting a pattern from the
  106.         System PAT#, and filling the menu rectangles with that pattern
  107.     */
  108.     for (index = 1; index <= (PATROWS * PATCOLS); index++){
  109.         /* get the pattern                                 */
  110.         GetIndPattern(&thePat,LISTID,index);
  111.         /* calculate the rectangle in the menu             */
  112.         calcitemrect(menuRect,&itemRect,index);
  113.         /* draw the menu item                             */
  114.         InsetRect(&itemRect,1,1);
  115.         FillRect(&itemRect,thePat);
  116.         FrameRect(&itemRect);
  117.     }
  118. }
  119.  
  120. domenuchoose(theMenu,menuRect,hitPt,whichItem)
  121. MenuHandle    theMenu;
  122. Rect        *menuRect;
  123. Point        hitPt;
  124. int            *whichItem;
  125. {
  126.     int        index,itemChosen;
  127.     Rect    testRect,oldRect;
  128.     
  129.     
  130.     itemChosen = 1000;
  131.     /*
  132.         an impossible menu choice
  133.     */
  134.     
  135.     /*
  136.         find out which menu item rectangle the mouse is in
  137.     */
  138.     for (index=1; index<=(PATROWS*PATCOLS); index++){
  139.         calcitemrect(menuRect,&testRect,index);
  140.         if (PtInRect(hitPt,&testRect)){
  141.             /*
  142.                 the mouse is in the "indexth" menu item
  143.             */
  144.             itemChosen = index;
  145.             break;
  146.         }
  147.     }
  148.     if (itemChosen != 1000){
  149.         /*
  150.             then we found the mouse in a menu item.
  151.             was there an old menu item selected?
  152.         */
  153.         if ((*whichItem != 0) && (*whichItem != itemChosen)){
  154.             /*
  155.                 if so, then un-select it
  156.             */
  157.             calcitemrect(menuRect,&oldRect,*whichItem);
  158.             PenMode(patXor);
  159.             FrameRect(&oldRect);
  160.         }
  161.         /* 
  162.             tell the menu manager which item was last selected
  163.         */
  164.         *whichItem = itemChosen;
  165.         /*
  166.             and select it
  167.         */
  168.         calcitemrect(menuRect,&oldRect,itemChosen);
  169.         PenMode(patCopy);
  170.         FrameRect(&oldRect);
  171.     }
  172.     else{
  173.         /*  
  174.             no menu item was selected, so un-select any previous
  175.             menu item selected                                    
  176.         */
  177.         if (!PtInRect(hitPt,menuRect)){
  178.             calcitemrect(menuRect,&oldRect,*whichItem);
  179.             PenMode(patXor);
  180.             FrameRect(&oldRect);
  181.             *whichItem = 0;
  182.         }
  183.     }
  184. }
  185.  
  186. domenusize(theMenu)
  187. MenuHandle        theMenu;
  188. {
  189.     /*
  190.         tell the menu manager how large the menu rectangle is
  191.     */
  192.     (**theMenu).menuWidth  = PATCOLS*PRECTWIDE;
  193.     (**theMenu).menuHeight = PATROWS*PRECTHIGH;
  194. }
  195.